package com.fmob.client.app.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;

import com.fmob.client.app.R;
import com.fmob.client.app.application.FMApplication;

import java.lang.ref.WeakReference;

import sdk.webview.fmc.com.fmcsdk.activity.LoginActivity;
import sdk.webview.fmc.com.fmcsdk.base.BaseActivity;
import sdk.webview.fmc.com.fmcsdk.base.BasePresenter;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.ImageUtils;
import sdk.webview.fmc.com.fmcsdk.util.Utility;


/**
 * 启动页界面
 */
public class WelcomeActivity extends BaseActivity {

    ImageView imageView1;
    /**是否登录*/
    private boolean islogin = false;

    /**
     * 创建静态内部类
     */
    private final MyHandler handler = new MyHandler(this);

    private  class MyHandler extends Handler {
        //持有弱引用WelcomeActivity,GC回收时会被回收掉.
        private final WeakReference<WelcomeActivity> mActivty;
        public MyHandler(WelcomeActivity activity){
            mActivty =new WeakReference<WelcomeActivity>(activity);
        }
        @Override
        public void handleMessage(Message msg) {
            WelcomeActivity activity=mActivty.get();
            super.handleMessage(msg);
            if(activity!=null){
                //执行业务逻辑
                // 1：登录 2：直接进入首页
                switch (msg.what) {
                    case 1:
                        activity.startActivity(new Intent(activity, LoginActivity.class));
                        activity.finish();
                        break;
                    case 2:
                        Utility.startLoginActivityForH5Home(WelcomeActivity.this);
                        activity.finish();
                        break;
                    default:
                        break;
                }
            }
        }
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
         imageView1 = (ImageView) findViewById(R.id.imageView1);
        /**动态设置启动页的背景图片*/
        Bitmap localBitmap = BitmapFactory.decodeFile(ImageUtils.getImagePath2(sharedPrefs.getString(Constant.START_BG,""),this));
        if (localBitmap!=null)
            imageView1.setImageBitmap(localBitmap);

        //关闭硬件加速，增加透明度渐变动画
        imageView1.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
        AlphaAnimation animation = new AlphaAnimation(0.0f,1.0f);
        animation.setDuration(2000);
        imageView1.setAnimation(animation);
        islogin = sharedPrefs.getBoolean(Constant.ISLOGIN,false);
        ( (FMApplication)getApplication()).getExecutor().execute(runnable);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }


    // 停留几秒后跳转到相应的页面线程
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            try {
                // 停留2秒钟
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                logger.info("WelcomeActivity",e.getMessage());
            }

            // 是否设置了连接地址,没设置，去设置;是否登录，如果已登录，跳转到主界面;如果没有登录，则跳转到登录界面
            if (!islogin) {
                // 跳转到登录界面
                handler.sendEmptyMessage(1);
            } else {
                // 跳转到主界面
                handler.sendEmptyMessage(2);
            }
        }
    };

}
